package dbmethod

import (
	"fmt"
	"photoPlugIn/config"
	us "photoPlugIn/utils"
)

type PhotoCache struct {
	Id            int    `sqlField:"id" sqlType:"integer PRIMARY KEY autoincrement"`
	PhotoId       string `sqlField:"photoid" sqlType:"int(32)"`
	PhotoMD5      string `sqlField:"photomd5" sqlType:"CHAR(32)"`
	PhotoFileName string `sqlField:"photofilename" sqlType:"NVARCHAR(255)"`
	URl           string `sqlField:"url" sqlType:"CHAR(1)"`
	OneLevelName  string `sqlField:"onelevename" sqlType:"NVARCHAR(255)"`
	PhotoSize     string `sqlField:"photosize" sqlType:"CHAR(8)"`
}

func PhotoCahceInsert(md5 string) int64 {
	db := config.CONFIG.Conn
	stmt, err := db.Prepare("INSERT INTO photocache(photomd5,onelevename) values(?,?)")
	us.CheckErr(err)
	res, err := stmt.Exec(md5, config.CONFIG.PHOTODB)
	us.CheckErr(err)
	id, err := res.LastInsertId()
	us.CheckErr(err)
	// fmt.Println(id)
	return id
}

func PhotoCahceFind(md5 string) *PhotoCache {
	// fmt.Println("测试查找...")
	db := config.CONFIG.Conn
	selectStr := "photomd5=" + "'" + md5 + "'" + " AND " + "onelevename=" + "'" + config.CONFIG.PHOTODB + "'" + " limit 0,1"
	fmt.Println("sqlCmd:", "SELECT id,photomd5,filename FROM photocache WHERE "+selectStr)
	rows, err := db.Query("SELECT id,photomd5 FROM photocache WHERE " + selectStr)
	us.CheckErr(err)
	defer rows.Close()
	p := new(PhotoCache)
	for rows.Next() {
		err := rows.Scan(&p.Id, &p.PhotoMD5)
		us.CheckErr(err)
		fmt.Println(p.Id, p.PhotoMD5)
	}
	return p
}
